package org.example.mrrope.service;

import lombok.extern.slf4j.Slf4j;
import org.example.mrrope.utils.DingUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WuLiaoService {

    @Autowired
    private DingUtil dingUtil;

    @Async
    public Integer saveWuLiaoWorkOrder(String uuid) throws Exception {
        //刷新派工单
        //查询包装车间派工单
        String ss = dingUtil.getYidaById(uuid);
        JSONObject bomJsonObject = new JSONObject(ss);
//        JSONObject bomJsonObject = bomOneJson.getJSONObject("formData");
        String pgdId = bomJsonObject.has("serialNumberField_m2ua3wlv") ? bomJsonObject.getString("serialNumberField_m2ua3wlv") : null;
        String so = bomJsonObject.has("textField_luga5zz1") ? bomJsonObject.getString("textField_luga5zz1") : null;
        String ac = bomJsonObject.has("textField_lwt597ig") ? bomJsonObject.getString("textField_lwt597ig") : null;
        String userId = bomJsonObject.has("textField_lwt4miut") ? bomJsonObject.getString("textField_lwt4miut") : null;
        Double sulian = bomJsonObject.has("numberField_lwt597ij") ? bomJsonObject.getDouble("numberField_lwt597ij") : null;
        //判断该派工单是否有缴库数据
        JSONObject searchJoGX = new JSONObject();
        //-------
        JSONObject searchJo = new JSONObject();
        searchJo.put("textField_lupafwsb", pgdId);
        searchJo.put("textField_m2d0pu16", so);
        searchJo.put("textField_lwt597ig", ac);
        searchJo.put("textField_lwt4miut", userId);//客户名称
        String ss2 = dingUtil.yiDaSearchReturnData("FORM-66A0D556664246B69167368F08A4726BCMYJ", searchJo);
        JSONArray bomOneJson2 = new JSONArray(ss2);
        if (bomOneJson2.length() > 0) {
            Double je2 = 0.0;
            for (int j = 0; j < bomOneJson2.length(); j++) {
                JSONObject bomJsonObject2 = bomOneJson2.getJSONObject(j).getJSONObject("formData");
                if (bomJsonObject2.has("numberField_m2co9jl5")) {
                    je2 = je2 + bomJsonObject2.getDouble("numberField_m2co9jl5");
                }
            }
            searchJoGX.put("numberField_m5f5gz4u", je2);
            searchJoGX.put("numberField_m2ucxjgr", sulian - je2);
        } else {
            searchJoGX.put("numberField_m5f5gz4u", 0);
            searchJoGX.put("numberField_m2ucxjgr", sulian);
        }
        //---------
        JSONObject searchJo3 = new JSONObject();
        searchJo3.put("textField_m2dfkkre", pgdId);
        searchJo3.put("textField_m2cokczz", so);
        searchJo3.put("textField_m2cokczw", ac);
        searchJo3.put("textField_m2cokd00", userId);//客户名称
        String ss3 = dingUtil.yiDaSearchReturnData("FORM-43D5985B0AC9495093B7F60F95BE8A07Y05F", searchJo3);
        JSONArray bomOneJson3 = new JSONArray(ss3);
        if (bomOneJson3.length() > 0) {
            Double je2 = 0.0;
            for (int j = 0; j < bomOneJson3.length(); j++) {
                JSONObject bomJsonObject2 = bomOneJson3.getJSONObject(j).getJSONObject("formData");
                if (bomJsonObject2.has("numberField_m2diqzka")) {
                    je2 = je2 + bomJsonObject2.getDouble("numberField_m2diqzka");
                }

            }
            searchJoGX.put("numberField_m5f5gz4w", je2);
            searchJoGX.put("numberField_m5f5gz4x", sulian - je2);
        } else {
            searchJoGX.put("numberField_m5f5gz4w", 0);
            searchJoGX.put("numberField_m5f5gz4x", sulian);
        }
        dingUtil.UpdateYiDa(uuid, searchJoGX);
        log.info("刷新数据----------{}--------", "成功");
//        }


        //查询当前物料是否存在
//        JSONObject searchScheduleJson = new JSONObject();
//        searchScheduleJson.put("selectField_lup8v6zm", upJsonA.has("selectField_lup8v6zm") ? upJsonA.get("selectField_lup8v6zm") : "");//类型
//        searchScheduleJson.put("textField_lwg986u6", upJsonA.has("textField_lwg986u6") ? upJsonA.get("textField_lwg986u6") : "");//编码
//        String yiDaSearchReturnData_Schedule = dingUtil.yiDaSearchReturnData("FORM-20BC9EAEC8E64BA2A3879B7BBB9E4DBCH5EJ", searchScheduleJson);//获取柬埔寨物料数据
//        JSONArray yiDaSearchReturnData_ScheduleJsonArray = new JSONArray(yiDaSearchReturnData_Schedule);
//        if (!yiDaSearchReturnData_ScheduleJsonArray.isEmpty()) {
//            String oid = yiDaSearchReturnData_ScheduleJsonArray.getJSONObject(0).getString("formInstanceId");
//            dingUtil.UpdateYiDa(oid, upJsonA);
//            log.info("更新物料数据完成");
//        }else{
//            dingUtil.saveYiDa("FORM-3A8D681A77094B6DAF95EBB52ABF301DGFLW", upJsonA, userId);//新增生产计划
//            log.info("新增物料划数据完成");
//        }
//        //不存在则新增
//        //存在则修改
//        log.info("开始执行保存物料工单");
        return 1;
    }
}
